package com.shujia.controller;

import com.shujia.entity.Emp;
import com.shujia.service.impl.EmpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.time.Duration;
import java.util.List;

@Controller
@RequestMapping("emp")
public class EmpController {

    @Autowired
    private EmpServiceImpl empService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("add")
    public String save1() {
        return "add";
    }

    @PostMapping("add2")
    public String save2(Emp emp, Model model) {
        empService.save(emp);
        List<Emp> emps = empService.findAll();
        model.addAttribute("emps", emps);
        return "index";
    }

    @RequestMapping("/delete/{empno}")
    public String delte(@PathVariable("empno") String empno, Model model) {
        empService.delete(empno);
        System.out.println("delete方法执行成功！！");
        List<Emp> emps = empService.findAll();
        model.addAttribute("emps", emps);
        return "index";
    }

    @RequestMapping("/updatePage/{empno}")
    public String updatePage(@PathVariable("empno") String empno, Model model) {
        Emp emp = empService.get(empno);
        model.addAttribute("emp", emp);
        return "update";
    }

    @PostMapping("update")
    public String updateEmp(Emp emp, Model model) {
        Emp emp1 = empService.get(emp.getEmpno());
        System.out.println("被修改前的员工：" + emp1);
        empService.update(emp);
        List<Emp> emps = empService.findAll();
        model.addAttribute("emps", emps);
        return "index";
    }

    @PostMapping("/showTopone")
    public String showTopone(String empno,Model model){
        //先去redis查询
        String value = stringRedisTemplate.opsForValue().get(empno);
        System.out.println(value);
        if(value==null){
            System.out.println("=======================走mysql===========================");
            //如果redis中不存在该员工，就去mysql查，查到后返回给前端，并且往redis中插入该数据
            //empno作为key,剩下的数据拼接组成字符串作为值
            Emp emp = empService.get(empno);

            //将值拼出来
            StringBuffer sb = new StringBuffer();
            sb.append(emp.getEname()).append(",").append(emp.getJob())
                    .append(",").append(emp.getMgr())
                    .append(",").append(emp.getHiredate())
                    .append(",").append(emp.getSal())
                    .append(",").append(emp.getComm())
                    .append(",").append(emp.getDeptno());
            String value2 = sb.toString();

            //添加到redis中
            stringRedisTemplate.opsForValue().set(empno,value2);
            //并且设置过期时间
            stringRedisTemplate.expire(empno, Duration.ofMinutes(1L));

            //数据返回给前端
            model.addAttribute("empone",emp);
            return "showTopone";
        }else {
            System.out.println("==================直接走redis,不走mysql=========================");
            String[] strings = value.split(",");

            Emp emp = new Emp(empno, strings[0], strings[1], strings[2], strings[3], strings[4], strings[5], strings[6]);
            //数据返回给前端
            model.addAttribute("empone",emp);
            return "showTopone";
        }



    }


}
